﻿<?php
	if(isset($_GET["id"])){
	
		$id = $_GET["id"];
		
		$xml = new DomDocument();
		$xml->load('../blog.xml');
		
		$posts  = $xml->getElementsByTagName('post');
		$count  = $posts->length;
		
		$comentarios  = $xml->getElementsByTagName('comentario');
		$comentCount  = $comentarios->length;

		for($i = $count-1; $i >= 0; $i--){//Efetuando delete com o for
			
			$p = $posts->item($i);
			$idPost = $p->getAttribute('idPost');
			if($id == $idPost){//Deleta os post visualizado
				$parent = $p->parentNode;
				$parent->removeChild($p);
				
				for($j = $comentCount-1; $j >= 0; $j--){
					$pc = $comentarios->item($j);
					print_r($pc);
					$idPostComent = $pc->getAttribute('idPostComentario');
					if($id == $idPostComent){//remove todos os comentarios do post
						$parentComent = $pc->parentNode;
						$parentComent->removeChild($pc);
					}
				}
			}
		}				
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		if($xml->save('../blog.xml')){
			echo "<script>alert('operação realizada com sucesso'); 
			              window.location = 'listarPost.php';
			     </script>";
		}
	}
?>